app.controller("transfer-dailyController",function($scope, $http,$routeParams){
    $scope.pact_id=$routeParams.id;
    $scope.class_name=$routeParams.class_name;
    $scope.class_id=$routeParams.class_id
    console.log($scope.class_id);
    // 清除cookie
    delCookie("cookie_clazz_enrol_id");
    function delCookie($name){
        var myDate=new Date();
        myDate.setTime(-1000);//设置时间
        document.cookie=$name+"=''; expires="+myDate.toGMTString();
    }
    $("#breaks").val((Number($("#sale_tuition").html)-Number($("#tuition_total").html()))*(1-Number($("#treatment").val())));
    $("#subjectname").change(function () {
        var  subject_id=$(this).val();
        $http({
            method:"POST",
            url:"/OA/classsubject/list",
            data:$.param({class_id:$scope.class_id,subject:subject_id}),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            console.log(response.data)
            $scope.datas=response.data;
        })
        $scope.add=function($index){
            var subject_name=$("#addname_"+$index+"").html();
            var subject_2_id=$("#addname_"+$index+"").attr("textval");
            var price=$("#addprice_"+$index+"").val();
            var number=$("#number_"+$index+"").val();
            var staff_name=$("#name_"+$index+"").html();
            var staff_id=$("#name_"+$index+"").attr("textval");
            var hour_secondary=$("#hour_"+$index+"").val();
            var tuition=$("#zongprice_"+$index+"").val();
            var clazz_enrol_id=$("#orderno").val();
            if(clazz_enrol_id==""){
                alert("请填写报课单号");
                return
            }
            var  method_id=$("#methostyle").find("option:selected").val();
            var method_name=$("#methostyle").find("option:selected").text();
           /*从cookie种获取数据，如果没有就调用接口获取 然后存到本地*/
            var clazz_enrol_id=getCookie("cookie_clazz_enrol_id");
            if(confirm("是否报"+subject_name+"的课程")){
                if(clazz_enrol_id==undefined){
                    $.ajax({
                        type:"POST",
                        url:"/OA/enrolclazz/create",
                        async: false,
                        data:{pact_id:$scope.pact_id,sn:clazz_enrol_id,class_id:$scope.class_id,method_id:method_id,method_name:method_name},
                        dataType : 'json',
                        success:function (response) {
                            if(response.ok==true){
                                document.cookie = 'cookie_clazz_enrol_id='+response.data;
                            }
                        }
                    })
                }
                clazz_enrol_id=getCookie("cookie_clazz_enrol_id");
                console.log(clazz_enrol_id)
                if(clazz_enrol_id!=undefined){
                    var  subject=$("#subjectname").find("option:selected").val();
                    $scope.json={clazz_enrol_id:clazz_enrol_id,class_id:$scope.class_id,subject:subject,subject_id:subject_2_id,subject_name:subject_name,price:price,number:number,staff_id:staff_id,staff_name:staff_name,hour_secondary:hour_secondary,tuition:tuition};
                    console.log($scope.json);
                    $.ajax({
                        type:"POST",
                        url:"/OA/subjectenrolclazz/create",
                        async: false,
                        data:$scope.json,
                        success:function (response) {
                            console.log(response)
                        }
                    })
                }
            }
            // 获取cookie的值;
            function getCookie(cookie_name){
                var allcookies = document.cookie;
                var cookie_pos = allcookies.indexOf(cookie_name);
                if (cookie_pos != -1){
                    cookie_pos += cookie_name.length + 1;
                    var cookie_end = allcookies.indexOf(";", cookie_pos);
                    if (cookie_end == -1){cookie_end = allcookies.length;}
                    var value = unescape(allcookies.substring(cookie_pos, cookie_end));}
                return value;
            }
               $http({
                    method:"POST",
                    url:"/OA/subjectenrolclazz/list",
                    data:$.param({clazz_enrol_id:clazz_enrol_id}),
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).success(function (response) {
                    console.log(response)
                    $scope.prods=response.data;
                   // 删除显示数据
                   $scope.delete=function ($index) {
                       $http({
                           method:"POST",
                           url:"/OA/subjectenrolclazz/delete",
                           data:$.param({id:$scope.prods[$index].id}),
                           headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                       }).success(function (response) {
                           if(response.ok==true){
                               $scope.prods.splice($index,1);
                           }
                       })
                   }
                   // 优惠切换
                   $("#saleclass").change(function () {
                       $("#treatment").val($(this).find("option:selected").attr("data"))
                   })
                   // 求总和;
                   $scope.computations=function () {
                       var array=[];
                       for(var i=0 ; i<$scope.prods.length ; i++){
                           array.push($scope.prods[i].tuition)
                       }
                       console.log(array);
                       Array.prototype.sum = function (){
                           var result = 0;
                           for(var i = 0; i < this.length; i++) {
                               result += this[i];
                           }
                           return result;
                       };
                       var sum=array.sum();
                       $("#now_tuition").val(sum);
                       var tuition_total=Number($("#tuition_total").text());
                       var  Should=(sum+tuition_total)*Number($("#treatment").val())-Number($("#breaks").val())-Number($("#tuition_reality").html());//应缴费用
                       $("#Should").val(Should);
                       $("#owe").val(Should-Number($("#actual").val()));

                   }
                })

        }
        $scope.resetting=function ($index) {
            $("#addprice_"+$index+"").val("");
            $("#number_"+$index+"").val("");
            $("#hour_"+$index+"").val("");
            $("#zongprice_"+$index+"").val("");
            $("#btnstyle_"+$index+"").css({display:"block"});
            $("#btnprystyle_"+$index+"").css({display:"none"});
        }
    })
    //提交数据报课
    $scope.save=function () {
        var  sn=$("#orderno").val();
        var  method_name=$("#methostyle").find("option:selected").text();
        var  method_id=$("#methostyle").find("option:selected").val();
            if(method_id=="0"){
            alert("请选择缴费方式");
            return;
        }
        var  class_id=$scope.class_id;
        var  principal=$("#headmaster").val();
        var  parents=$("#parents").val();
        var  tuition_total=Number($("#now_tuition").val())+Number($("#tuition_total").html());
        var  tuition_should=Number($("#Should").val());
        var  tuition_reality=Number($("#actual").val())+Number($("#tuition_reality").html());
        var  tuition_lack=Number($("#owe").val());
        var  sale_id=$("#saleclass").find("option:selected").val();
        var  sale_name=$("#saleclass").find("option:selected").text();
        var  sale_derate=Number($("#breaks").val());
        var  sale_tuition=(1-Number($("#treatment").val()))*tuition_total+sale_derate;
        var  this_tuition_total=Number($("#now_tuition").val());
        var  this_tuition_reality=Number($("#actual").val());
        var  record_sn=$("#record_sn").val();
        clazz_enrol_id=getCookie("cookie_clazz_enrol_id");
        $scope.save={sn:sn, method_name:method_name,method_id:method_id,class_id:class_id,principal:principal,parents:parents,tuition_total:tuition_total,tuition_reality:tuition_reality,tuition_lack:tuition_lack,id:clazz_enrol_id,tuition_should:tuition_should,sale_id:sale_id,sale_name:sale_name,sale_tuition:sale_tuition,this_tuition_total:this_tuition_total,this_tuition_reality:this_tuition_reality,sale_derate:sale_derate,record_sn:record_sn};
        console.log($scope.save)
        $http({
            method:"POST",
            url:"/OA/enrolclazz/update",
            data:$.param($scope.save),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            console.log(response)
            alert(response.ok)
        })
    }
    // 学员信息展示
    $http({
        method:"POST",
        url:"/OA/clazz/query ",
        data:$.param({id:$scope.pact_id}),
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response){
        $scope.findstudent=response.data;
        $("#treatment").val(response.data.sale);
        if(response.data.tuition_total==0){
            $("#breaks").val("0");
        }else{
            var tempNaN=response.data.tuition_total*response.data.sale-response.data.tuition_reality-response.data.tuition_lack;
            $("#breaks").val(tempNaN+"");
        }


    })
    //班课报课情况
    $http({
        method:"POST",
        url:"/OA/subjectpactclazz/list ",
        data:$.param({pact_id:$scope.pact_id}),
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response){
        console.log(response)
        $scope.subjectpactclazz=response.data;
    })
    // 报课增加
})

